/*
 *   AS IS License
 * ==Naive Coders==
 */
package org.naive.metawatch.converter.parser.cap;

import java.util.ArrayDeque;
import java.util.Deque;
import org.naive.metawatch.converter.parser.ParseContext;

/**
 *
 * @author gbu.000
 */
public class CapParseContext extends ParseContext {

    private final Deque<Block> blocks = new ArrayDeque<>();

    public CapParseContext(byte[] data) {
        super(data);
    }

    public boolean isAtBlockBoundary() {
        if (!blocks.isEmpty()) {
            final Block b = blocks.peek();
            assert getOffset() < (b.offset + b.size);
            return getOffset() == b.offset + b.size;
        }
        return false;
    }

    public void startBlock(int size) {
        blocks.push(new Block(getOffset(), size));
    }

    public void endBlock() {
        final Block b = blocks.pop();
        advanceOffset(b.offset + b.size - getOffset());
    }

    private static class Block {

        private final int offset;
        private final int size;

        public Block(int offset, int size) {
            this.offset = offset;
            this.size = size;
        }
    }
}
